/*
 * Copyright (C) 2016 yixun All Rights Reserved.
 * 
 * ProjectRoomServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emm.yixun.basic.mapper.ProjectMapper;
import com.emm.yixun.basic.mapper.ProjectRoomMapper;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.common.model.PmoSaleData;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.dto.PmoSaleDataDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.pmo.PicHighchartsVo;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.utils.MathUtil;

/**
 * [文件名称]<br>
 * ProjectRoomServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 房产房间表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2016-05-11 11:27:19 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("projectRoomService")
public class ProjectRoomServiceImpl implements ProjectRoomService {

	@Autowired
	private ProjectRoomMapper mapper;
	
	@Autowired
	private ProjectMapper projectMapper;

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<ProjectRoom> getPageModel(ProjectRoomDto entity, int begin, int end) {
		Integer total = mapper.count(entity);
		List<ProjectRoom> rows = mapper.list(entity, begin, end);
		Page<ProjectRoom> msr = new Page<ProjectRoom>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<ProjectRoom> selectListByDto(ProjectRoomDto entity) {
		return mapper.selectListByDto(entity);
	}

	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public ProjectRoom selectByDto(ProjectRoomDto entity) {
		return mapper.selectByDto(entity);
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public ProjectRoom selectById(Long id) {
		if (id != null) {
			return mapper.selectById(id);
		}
		return null;
	}

	/**
	 * @param entity
	 *            新增记录的详细信息
	 * @return 新增行数
	 */
	public ProjectRoomDto create(ProjectRoomDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity
	 *            更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(ProjectRoomDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}

	public int updates(ProjectRoomDto entity, String ids) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edits(entity, ids);
	}

	/**
	 * @param entity
	 *            待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(ProjectRoomDto entity) {
		return mapper.del(entity);
	}

	/**
	 * @param entity
	 *            新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(ProjectRoomDto entity) {
		if (entity != null && entity.getId() != null) {
			return update(entity);
		} else {
			create(entity);
			return 1;
		}
	}

	/**
	 * 查询列表
	 * 
	 * @param ids
	 *            搜索条件
	 * @return 记录
	 */
	public List<ProjectRoom> queryListByIds(String ids) {
		return mapper.queryListByIds(ids);
	}

	public int selectCountByDto(ProjectRoomDto entity) {
		Integer total = mapper.count(entity);
		return total == null ? 0 : total.intValue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.emm.yixun.basic.service.ProjectRoomService#getPageDtoModel(com.emm.yixun.common.model.dto.ProjectRoomDto,
	 * int, int)
	 */
	public Page<ProjectRoomDto> getPageDtoModel(ProjectRoomDto entity, int begin, int end) {
		Integer total = mapper.countDto(entity);
		List<ProjectRoomDto> rows = mapper.listDto(entity, begin, end);
		Page<ProjectRoomDto> msr = new Page<ProjectRoomDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	/**
	 * 根据项目ID查询项目房间业务数据
	 * 
	 * @param projectId
	 * @return ProjectRoom
	 */
	public ProjectRoom selectProjectRoom(Long projectId) {
		return mapper.selectProjectRoom(projectId);
	}

	public List<String> groupByIndustMark(Long premisesId) {
		// TODO Auto-generated method stub
		return mapper.groupByIndustMark(premisesId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.emm.yixun.basic.service.ProjectRoomService#updateCombinedNameByPid(com.emm.yixun.common.model.dto.ProjectRoomDto
	 * )
	 */
	public void updateCombinedNameByPid(ProjectRoomDto entity) {
		mapper.updateCombinedNameByPid(entity);
	}

	public ProjectRoomDto selectRoomInfoSummary(Long buildingId, Long isRoom) {
	    return mapper.selectRoomInfoSummary(buildingId, isRoom);
	}

	public List<ProjectRoom> queryRoomIndustGroup(ProjectRoomDto entity) {
	    return mapper.queryRoomIndustGroup(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.emm.yixun.basic.service.ProjectRoomService#getPageDtoModel(com.emm.yixun.common.model.dto.ProjectRoomDto,
	 * int, int)
	 */
	public Page<ProjectRoomDto> getProjectRoomInfoPage(ProjectRoomDto entity, int begin, int end) {
		Integer total = mapper.projectRoomInfoCount(entity);
		List<ProjectRoomDto> rows = mapper.projectRoomInfoList(entity, begin, end);
		Page<ProjectRoomDto> msr = new Page<ProjectRoomDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.emm.yixun.basic.service.ProjectRoomService#getPageDtoModel(com.emm.yixun.common.model.dto.ProjectRoomDto,
	 * int, int)
	 */
	public Page<ProjectRoomDto> getCustomerRoomPage(ProjectRoomDto entity, int begin, int end) {
		Integer total = mapper.customerRoomCount(entity);
		List<ProjectRoomDto> rows = mapper.customerRoomList(entity, begin, end);
		Page<ProjectRoomDto> msr = new Page<ProjectRoomDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}
	
	/**
	 * 修复错误数据功能
	 * 根据分期、楼栋、单元、房间名称获取实际房间
	 * @param entity
	 * 
	 */
	public List<ProjectRoom> selectRepairDataRoomListByDto(ProjectRoomDto entity) {
		return mapper.selectRepairDataRoomListByDto(entity);
	}

	public List<PicHighchartsVo> findIndustData(Long projectId) {
		
		return mapper.findIndustData(projectId);
	}

	public BigDecimal getRemovalRate(String projectName) {
		ProjectDto projectDto = new ProjectDto();
		projectDto.setProjectName(projectName);
		Project project = projectMapper.selectByDto(projectDto);
		if(project!=null) {
			BigDecimal area = mapper.queryProjectArea(project.getId());
			PmoSaleDataDto saleData = new PmoSaleDataDto();
			saleData.setProjectId(project.getId());
			PmoSaleData pmoSaleData =projectMapper.queryPmoSaleData(saleData);
			if(pmoSaleData!=null && pmoSaleData.getTotalCalculateArea()!=null &&
					area!=null && area.compareTo(BigDecimal.ZERO)!=0) {
				BigDecimal result = MathUtil.bigDivide(pmoSaleData.getTotalCalculateArea(), area, 2);
				return new BigDecimal(MathUtil.multiply(new Double(result.toString()), new Double("100")));
				
			}
		}
		
		return null;
	}
}
